package cn.jsp.jdbc;

import lombok.SneakyThrows;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.lang.reflect.Field;
public class GeneralRowProcessor<T> implements RowProcessor<T>{
  Class<T> target;

  public GeneralRowProcessor(Class<T> target) {
    this.target = target;
  }

  @Override @SneakyThrows
  public T process(ResultSet rs) throws SQLException {
    //new
    T obj=target.getDeclaredConstructor().newInstance();
    //set
    for(Field field:target.getDeclaredFields()){
      field.setAccessible(true);
      String name=field.getName();
      field.set(obj,rs.getObject(name));
    }
    return obj;
  }
}
